<?php

namespace App\Admin\Extensions;

use App\Models\Statistic;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Layout\Column;
use Encore\Admin\Widgets\InfoBox;
use App\Services\Statistics\SearchCount;
use Carbon\Carbon;
use Brick\Math\RoundingMode;


class GoldStatisticsBoxes
{
	public function __construct()
	{
		$this->start = request('start') ?? Carbon::today()->toDateString();
		$this->end = request('end') ?? Carbon::tomorrow()->toDateString();
		$this->gold_round_id = request('gold_round_id') ?? 0;
		$this->min = request('min') ?? 0;
		$this->max = request('max') ?? 1000;
		$this->result = ['start'=> $this->start , 'end'=> $this->end , 'gold_round_id' => $this->gold_round_id, 'min' => $this->min, 'max' => $this->max];
		
	}

	public function countBoxes()
	{
		return function (Row $row) {
			
			$this->goldChargeMoney($row, 2);
			$this->withdrawMoney($row, 2);
			$this->goldBeansFee($row, 2);
			$this->userGoldCoins($row, 2);
			$this->userSupportGoldCoins($row, 2);
			$this->userGoldBeans($row, 2);
			$this->userSupportGoldBeans($row, 2);
			$this->productCost($row, 2);
			$this->productCostTotal($row, 2);
			$this->goldRecordsCount($row, 2);
			$this->goldRecordsMoney($row, 2);
			$this->goldRecordsUserCount($row, 2);
			$this->goldRecordsAverageMoney($row, 2);
			$this->goldRecordsAverageCount($row, 2);
			$this->goldCoinsDirectPrize($row, 2);
			$this->goldCoinsTeamPrize($row, 2);
			$this->goldSubsidiesUnlocked($row, 2);
			$this->goldCashesBuy($row, 2);
			$this->goldCashesFromBeans($row, 2);
			$this->userGrowth($row, 2);
			$this->userTotalAmount($row, 2);
			$this->userGoldLevelGreaterThanZeroTotalAmount($row, 2);
			$this->userCandidateTotalAmount($row, 2);
			$this->companyIncome($row, 2);
			$this->companyCost($row, 2);
			$this->companyProfit($row, 2);
			$this->goldBeansProductPrice($row, 2);
			$this->goldBeansOverTime($row, 2);
			$this->withdrawChargeMoney($row, 2);
			$this->alipayChargeMoney($row, 2);
			$this->goldRecordsMoneyRange($row, 2);
			$this->alipayWithdrawMoney($row, 2);
			$this->bankWithdrawMoney($row, 2);
			$this->groundingProduct($row, 2);
			$this->pendingSplit($row, 2);
		};
	}
	
	protected function goldChargeMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchGoldChargesMoney($this->result);
			$box = new InfoBox('充值总额', 'bitcoin', 'purple', '/admin/gold-records', $money);
			$column->append($box);
		});
	}

	protected function withdrawMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchWithdrawActualMoney($this->result);
			$box = new InfoBox('提现总额', 'bitcoin', 'purple', '/admin/withdraws', $money);
			$column->append($box);
		});
	}

	protected function goldBeansFee(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchGoldBeansFee($this->result);
			$box = new InfoBox('寄售金豆', 'bitcoin', 'purple', '/admin/gold-beans', $money);
			$column->append($box);
		});
	}

	protected function userGoldCoins(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = big_decimal(SearchCount::searchUserGoldCoins())
					->minus(SearchCount::searchUserSupportGoldCoins());
			$box = new InfoBox('用户真实余额总数', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function userSupportGoldCoins(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchUserSupportGoldCoins();
			$box = new InfoBox('用户扶持余额总数', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}


	protected function userGoldBeans(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = big_decimal(SearchCount::searchUserGoldBeans())
					->minus(SearchCount::searchUserSupportGoldBeans());
			$box = new InfoBox('用户购买金豆总数', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function userSupportGoldBeans(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchUserSupportGoldBeans();
			$box = new InfoBox('用户扶持金豆总数', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function productCost(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money =SearchCount::searchProductCostIsDelivered($this->result);
			$box = new InfoBox('已发货成本统计', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function productCostTotal(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchProductCost($this->result);
			$box = new InfoBox('全部发货成本统计', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function goldRecordsCount(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$count = SearchCount::searchGoldRecordsCount($this->result);
			$box = new InfoBox('抢购总单数', 'users', 'green', '/admin/gold-records', $count);
			$column->append($box);
		});
	}

	protected function goldRecordsMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchGoldRecordsMoney($this->result);
			$box = new InfoBox('抢购总金额', 'bitcoin', 'purple', '/admin/gold-records', $money);
			$column->append($box);
		});
	}

	protected function goldRecordsUserCount(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$count = SearchCount::searchGoldRecordsUserCount($this->result);
			$box = new InfoBox('抢购总人数', 'users', 'green', '/admin/gold-records', $count);
			$column->append($box);
		});
	}

	protected function goldRecordsAverageMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = !empty(SearchCount::searchGoldRecordsUserCount($this->result)) ? 
						big_decimal(SearchCount::searchGoldRecordsMoney($this->result))->dividedBy(SearchCount::searchGoldRecordsUserCount($this->result), 2 , RoundingMode::HALF_UP) : 0;
			$box = new InfoBox('平均抢购金额', 'bitcoin', 'purple', '/admin/gold-records', $money);
			$column->append($box);
		});
	}

	protected function goldRecordsAverageCount(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$count = !empty(SearchCount::searchGoldRecordsUserCount($this->result)) ? 
						big_decimal(SearchCount::searchGoldRecordsCount($this->result))->dividedBy(SearchCount::searchGoldRecordsUserCount($this->result), 2 , RoundingMode::HALF_UP) : 0;
			$box = new InfoBox('平均抢购单数', 'users', 'green', '/admin/gold-records', $count);
			$column->append($box);
		});
	}

	protected function goldCoinsDirectPrize(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchGoldCoinsDirectPrize($this->result);
			$box = new InfoBox('直推奖励', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}
	
	protected function goldCoinsTeamPrize(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchGoldCoinsTeamPrize($this->result);
			$box = new InfoBox('团队奖励', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}

	protected function goldSubsidiesUnlocked(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = abs(SearchCount::searchGoldSubsidiesUnlocked($this->result));
			$box = new InfoBox('解锁补贴金', 'bitcoin', 'purple', '/admin/gold-subsidies', $money);
			$column->append($box);
		});
	}

	protected function goldCashesBuy(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = abs(SearchCount::searchGoldCashesBuy($this->result));
			$box = new InfoBox('兑换抢购金本金', 'bitcoin', 'purple', '/admin/gold-cashes', $money);
			$column->append($box);
		});
	}

	protected function goldCashesFromBeans(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = abs(SearchCount::searchGoldCashesFromBeans($this->result));
			$box = new InfoBox('兑换抢购金兑换金额', 'bitcoin', 'purple', '/admin/gold-cashes', $money);
			$column->append($box);
		});
	}


	protected function userGrowth(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$count = SearchCount::searchUserGrowth($this->result);
			$box = new InfoBox('用户增长', 'users', 'green', '/admin/user', $count);
			$column->append($box);
		});
	}

	protected function userTotalAmount(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$count = SearchCount::searchUserTotalAmount();
			$box = new InfoBox('用户总人数', 'users', 'green', '/admin/user', $count);
			$column->append($box);
		});
	}

	protected function userGoldLevelGreaterThanZeroTotalAmount(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$count = SearchCount::searchUserGoldLevelGreaterThanZeroTotalAmount();
			$box = new InfoBox('炼金师总人数', 'users', 'green', '/admin/user', $count);
			$column->append($box);
		});
	}

	protected function userCandidateTotalAmount(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$count = big_decimal(SearchCount::searchUserCandidateTotalAmount())
					->minus(SearchCount::searchUserGoldLevelGreaterThanZeroTotalAmount());
			$box = new InfoBox('见习炼金师总人数', 'users', 'green', '/admin/user', $count);
			$column->append($box);
		});
	}

	protected function companyIncome(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = big_decimal(SearchCount::searchProductPriceTotal($this->result))
					->minus(SearchCount::searchGoldProductPrice($this->result))
					->plus(SearchCount::searchGoldBeansFeeTotal($this->result))
					->plus(SearchCount::searchWithdrawChargeMoney($this->result))
					->plus(abs(SearchCount::searchGoldCoinsOverTime($this->result)))
					->plus(abs(SearchCount::searchGoldCashesBuy($this->result)));
			$box = new InfoBox('公司收入统计', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}

	protected function companyCost(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = big_decimal(SearchCount::searchGoldCoinsDirectPrize($this->result))
					->plus(SearchCount::searchGoldCoinsTeamPrize($this->result))
					->plus(abs(SearchCount::searchGoldSubsidiesUnlocked($this->result)))
					->plus(SearchCount::searchProductCost($this->result))
					->plus(big_decimal(SearchCount::searchGoldChargesMoney($this->result))->multipliedBy(0.006))
					->toScale(2, RoundingMode::HALF_UP);
			$box = new InfoBox('公司成本统计', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}

	protected function companyProfit(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money =  big_decimal(SearchCount::searchProductPriceTotal($this->result))
					->minus(SearchCount::searchGoldProductPrice($this->result))
					->plus(SearchCount::searchGoldBeansFeeTotal($this->result))
					->plus(abs(SearchCount::searchGoldCashesBuy($this->result)))
					->plus(SearchCount::searchWithdrawChargeMoney($this->result))
					->plus(abs(SearchCount::searchGoldCoinsOverTime($this->result)))
					->minus(SearchCount::searchGoldCoinsDirectPrize($this->result))
					->minus(SearchCount::searchGoldCoinsTeamPrize($this->result))
					->minus(abs(SearchCount::searchGoldSubsidiesUnlocked($this->result)))
					->minus(SearchCount::searchProductCost($this->result))
					->minus(big_decimal(SearchCount::searchGoldChargesMoney($this->result))->multipliedBy(0.006))
					->toScale(2, RoundingMode::HALF_UP);
			$box = new InfoBox('公司利润统计', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}

	protected function goldBeansProductPrice(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchGoldBeansPresent($this->result);
			$box = new InfoBox('平台赠送金豆总数', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function goldProductPrice(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchGoldProductPrice($this->result);
			$box = new InfoBox('黄金风爆商品销售金额', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function goldBeansOverTime(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = abs(SearchCount::searchGoldCoinsOverTime($this->result));
			$box = new InfoBox('罚豆金额', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}

	protected function withdrawChargeMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = abs(SearchCount::searchWithdrawChargeMoney($this->result));
			$box = new InfoBox('提现手续费总额', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}

	protected function alipayChargeMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = big_decimal(SearchCount::searchGoldChargesMoney($this->result))->multipliedBy(0.006)->toScale(2, RoundingMode::HALF_UP);
			$box = new InfoBox('阿里微信充值手续费', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}

	protected function goldRecordsMoneyRange(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$count = SearchCount::searchGoldRecordsMoneyRange($this->result);
			$box = new InfoBox('用户抢购范围人数', 'users', 'green', '/admin/gold-coins', $count);
			$column->append($box);
		});
	}

	protected function alipayWithdrawMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money =SearchCount::searchPendingWithdrawByAlipay($this->result);;
			$box = new InfoBox('支付宝待提现金额', 'bitcoin', 'purple', '/admin/withdraws', $money);
			$column->append($box);
		});
	}

	protected function bankWithdrawMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money =SearchCount::searchPendingWithdrawByBank($this->result);;
			$box = new InfoBox('银行卡待提现金额', 'bitcoin', 'purple', '/admin/withdraws', $money);
			$column->append($box);
		});
	}

	protected function groundingProduct(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money =SearchCount::searchGroundingProduct($this->result);;
			$box = new InfoBox('今日上架的商品', 'bitcoin', 'purple', '/admin/gold-products', $money);
			$column->append($box);
		});
	}

	protected function pendingSplit(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money =SearchCount::searchPendingSplit($this->result);;
			$box = new InfoBox('当日拆分的商品', 'bitcoin', 'purple', '/admin/gold-products', $money);
			$column->append($box);
		});
	}
}
